package cn.mengzhiyi.guimei.controller;

import cn.mengzhiyi.guimei.pojo.SuperUser;
import cn.mengzhiyi.guimei.service.LoginService;
import cn.mengzhiyi.guimei.service.impl.LoginServiceImpl;
import sun.nio.cs.US_ASCII;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet",urlPatterns="/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String userLoginName=request.getParameter("userLoginName");
        String userPassword=request.getParameter("userPassword");
        String action=request.getParameter("action");
        //管理员登录
        if (action.equals("userLogin")) {
            SuperUser sup = new SuperUser();
            sup.setUserLoginName(userLoginName);
            sup.setUserPassword(userPassword);
            LoginService loginService = new LoginServiceImpl();
            SuperUser result = loginService.login(sup);
            int msg = 1;
            if (result != null) {
                request.getSession().setAttribute("user", result);
                response.sendRedirect("/page/index.jsp");
            } else {
                response.sendRedirect("/Login.jsp?flag=" + msg);
            }
        }
        //商家登录
        else if (action.equals("cusLogin")){}
        //管理员注销
        else if (action.equals("out")){ }
        //管理员查询个人信息
        else if (action.equals("QueryById")){
            String userid=request.getParameter("id");
            String selectUpdate=request.getParameter("type");
            int id=Integer.parseInt(userid);
            LoginService loginService = new LoginServiceImpl();
            SuperUser result = loginService.getSuperUserByID(id);
                request.setAttribute("selectSuperUserById", result);
                if (selectUpdate.equals("select")){
                request.getRequestDispatcher("page/user/userQuery.jsp").forward(request,response);}
                else if (selectUpdate.equals("update")){
                    request.getRequestDispatcher("page/user/userUpdate.jsp").forward(request,response);}

        }
        //管理员信息更改
        else if (action.equals("userUpdate")){
            String update=request.getParameter("type");
            if (update.equals("updateOther")){
                String userid=request.getParameter("id");
                int id=Integer.parseInt(userid);
                String userName=request.getParameter("userName");
                String userID=request.getParameter("userId");
                String userLogiwnName=request.getParameter("userLoginName");

                SuperUser superUser=new SuperUser();
                superUser.setId(id);
                superUser.setUserName(userName);
                superUser.setUserID(userID);
                superUser.setUserLoginName(userLogiwnName);

                LoginService loginService = new LoginServiceImpl();
                int ir=loginService.updateOther(superUser);
                if (ir>0){
                response.sendRedirect("page/user/userQuery.jsp?ir="+ir);
                }


            }
            else if (update.equals("updatePwd")){

            }
        }

    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
